<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="queryParams.name" placeholder="输入姓名" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="queryParams.phone" placeholder="输入手机号" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="类型" prop="personnelTypeDict">
            <el-select v-model="queryParams.personnelTypeDict" placeholder="选择职别" clearable @change="handleQuery">
              <el-option v-for="dict in dict.type.biz_personnel_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="政治面貌" prop="politicalLandscapeDict">
            <el-select v-model="queryParams.politicalLandscapeDict" placeholder="选择政治面貌" clearable @change="handleQuery">
              <el-option v-for="dict in dict.type.biz_civil_servant_political_landscape" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="是否纳入绩效管理" prop="assessmentMark" label-width="auto">
            <el-switch v-model="queryParams.assessmentMark" active-text="是" inactive-text="否" @change="handleQuery"> </el-switch>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleEdit('add')" v-hasPermi="['organization:civil-servant:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['organization:civil-servant:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['organization:civil-servant:import']"
              >导入</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['organization:civil-servant:export']"
            >
              导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" type="index" align="center">
            <template slot-scope="scope">
              <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="部门"
            align="center"
            min-width="120"
            key="deptName"
            prop="deptName"
            v-if="columns[0].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="姓名" align="center" min-width="120" key="name" prop="name" v-if="columns[1].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-button type="text" @click="handleDetail(scope.row)">{{ scope.row.name }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="性别" align="center" key="sexDict" prop="sexDict" v-if="columns[4].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sexDict" />
            </template>
          </el-table-column>

          <el-table-column
            label="类型"
            align="center"
            min-width="100"
            key="personnelTypeDict"
            prop="personnelTypeDict"
            v-if="columns[2].visible"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.biz_personnel_type" :value="scope.row.personnelTypeDict" />
            </template>
          </el-table-column>
          <el-table-column
            label="政治面貌"
            align="center"
            min-width="130"
            key="politicalLandscapeDict"
            prop="politicalLandscapeDict"
            v-if="columns[3].visible"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.biz_civil_servant_political_landscape" :value="scope.row.politicalLandscapeDict" />
            </template>
          </el-table-column>
          <el-table-column
            label="编制性质"
            align="center"
            min-width="130"
            key="workNatureDict"
            prop="workNatureDict"
            v-if="columns[8].visible"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.biz_civil_servant_work_nature" :value="scope.row.workNatureDict" />
            </template>
          </el-table-column>
          <el-table-column label="职务" align="center" key="positionDict" prop="positionDict" v-if="columns[7].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag v-if="scope.row.positionDict" :options="dict.type.biz_civil_servant_position" :value="scope.row.positionDict" />
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column
            label="电话"
            align="center"
            min-width="120"
            key="phone"
            prop="phone"
            v-if="columns[6].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="是否纳入绩效管理" align="center" key="assessmentMark" prop="assessmentMark" v-if="columns[7].visible">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.assessmentMark"> </el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="特殊情况"
            align="center"
            min-width="140"
            key="specialSituationDict"
            prop="specialSituationDict"
            v-if="columns[5].visible"
          >
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.biz_civil_servant_special_situation"
                :value="scope.row.specialSituationDict"
                v-if="scope.row.specialSituationDict"
              />
              <span v-else>-</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleEdit('update', scope.row)"
                v-hasPermi="['organization:civil-servant:update']"
              >
                修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

    <EditModal :visible.sync="isEditOpen" :params="currentData" :operation="editType" @success="getList"></EditModal>
    <ImportModal :visible.sync="isImportOpen" :query-params="queryParams" @success="getList"></ImportModal>
    <DetailModal :visible.sync="isDetailOpen" :params="currentData"></DetailModal>
  </div>
</template>

<script>
import { list as listUser, remove } from '@/api/organization/civil-servant'
import { deptTreeSelect } from '@/api/system/user'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import EditModal from './components/Edit.vue'
import ImportModal from './components/Import.vue'
import DetailModal from './components/Detail.vue'
export default {
  name: 'civil-servant',
  dicts: [
    'biz_personnel_type',
    'sys_user_sex',
    'biz_civil_servant_political_landscape',
    'biz_civil_servant_special_situation',
    'biz_civil_servant_work_nature',
    'biz_civil_servant_position'
  ],
  components: { Treeselect, EditModal, ImportModal, DetailModal },
  data() {
    return {
      isDetailOpen: false,
      isImportOpen: false,
      // 当前操作数据
      currentData: {},
      // 编辑框
      isEditOpen: false,
      // 操作类型
      editType: 'add',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: '',
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        personnelTypeDict: undefined,
        deptId: undefined,
        politicalLandscapeDict: undefined,
        specialSituationDict: undefined,
        assessmentMark: true
      },
      // 列信息
      columns: [
        { key: 0, label: `部门`, visible: true },
        { key: 1, label: `姓名`, visible: true },
        { key: 2, label: `类型`, visible: true },
        { key: 3, label: `政治面貌`, visible: true },
        { key: 4, label: `性别`, visible: false },
        { key: 5, label: `特殊情况`, visible: false },
        { key: 6, label: `电话`, visible: true },
        { key: 7, label: `是否纳入绩效管理`, visible: true },
        { key: 8, label: `编制性质`, visible: true },
        { key: 9, label: `职务`, visible: true }
      ]
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
    this.getDeptTree()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleDetail(raw) {
      this.currentData = raw
      this.isDetailOpen = true
    },
    handleImport() {
      this.isImportOpen = true
    },
    handleEdit(type = 'add', raw = {}) {
      this.editType = type
      this.currentData = raw
      this.isEditOpen = true
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id
      this.handleQuery()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.queryParams.deptId = undefined
      this.$refs.tree.setCurrentKey(null)
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },

    /** 删除按钮操作 */
    handleDelete() {
      this.$confirm(`是否确认删除选中的 ${this.ids.length} 项数据？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            const loading = this.$loading({
              target: '.el-message-box',
              lock: true,
              text: '正在删除...',
              spinner: 'el-icon-loading',
              background: 'rgba(255,255,255,0.6)'
            })
            remove({ id: this.ids.join(',') })
              .then(() => {
                loading.close()
                this.getList()
                this.$modal.msgSuccess('已删除')
                done()
              })
              .catch(() => {
                loading.close()
                done()
              })
          } else {
            done()
          }
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        '/api/civilServant/excel/exports',
        {
          ...this.queryParams
        },
        `用户信息_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
